package com.cxd.base.util;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @Title: <br/>
 * @Description: <br/>
 * @Copyright: Copyright (c) 2015<br/>
 * @Company: aotime<br/>
 * @author WTJ
 * @version
 */
public class ProjecConfig {
	
	/* 项目名 如：pms*/
	 public final static String PROJECT_NAME;
	 
	 /* 项目基本路径 ：E:/AotimeSpace/pms */
	 public final static String PROJECT_BASE_PATH;
	 
	 /* 项目访问协议 */
	 public final static String PROJECT_SCHEME;
	 
	 /* 项目域名 */
	 public final static String SERVER_NAME;
	 
	 /* 项目端口号 */
	 public final static int SERVER_PORT;
	 
	 /* 项目访问跟目录 如：http://localhost:8080/pms*/
	 public final static String WEB_ROOT;
	 
	 /* 文件根目录 如：E:/AotimeSpaces/pms/WebRoot/*/
	 public final static String BASE_FIlE_PATH;
	 
	 
	 static{
			ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
			HttpServletRequest request = attr.getRequest();
			PROJECT_NAME = request.getContextPath();
			PROJECT_SCHEME = request.getScheme()+"://";
			SERVER_NAME = request.getServerName();
			SERVER_PORT = request.getServerPort();
			WEB_ROOT = PROJECT_SCHEME+SERVER_NAME+":"+SERVER_PORT+PROJECT_NAME;
			
			String thisFilePath = ProjecConfig.class.getResource("").getPath();
			try {
				thisFilePath = java.net.URLDecoder.decode(thisFilePath,"utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			if(thisFilePath.startsWith("file:")){
				thisFilePath = thisFilePath.substring(5);
			}
			int wbInt = thisFilePath.indexOf("WebRoot");
			if(wbInt < 0){
				wbInt = thisFilePath.indexOf(PROJECT_NAME);
			}
			
			PROJECT_BASE_PATH = thisFilePath.substring(0, wbInt);
			
			int tfp = thisFilePath.indexOf("WEB-INF");
			BASE_FIlE_PATH = thisFilePath.substring(0, tfp); 
	 }
}
